/**
<pageInfo>
  <currentPage>1</currentPage> 
  <itemCount>2</itemCount> 
  <itemCountPerPage>1</itemCountPerPage> 
  <pageCount>2</itemCountPerPage> 
  <listPageCount>10</listPageCount> 
  <listPageStart>1</listPageStart> 
  <listPageEnd>2</listPageEnd> 
  <startPoint>0</startPoint> 
  <fetchCount>1</fetchCount> 
  </pageInfo>
**/
dojo.provide("dojot.util.PageInfo");
dojo.experimental("dojot.util.PageInfo");

dojo.require("dojox.dtl.html");
dojo.require("dojox.dtl.Context");

dojo.declare("dojot.util.PageInfo",null,
	{
	items:[],
	currentPage: 1,			 // 当前页号(从1开始)
	itemCount:0,			 // 总记录数
	itemCountPerPage: 5,	 // 每页记录数
	pageCount: 0,			 // 总页数
	startPoint: 0,
	listPageCount:5,		 // 页码导航条中列出的页数 
	listPageStart:0,
	listPageEnd:0,
	templatePath: dojo.moduleUrl("dojot.util", "resources/pageInfo.html"),
	constructor: function(req){
		this.items = req.items;
		this.itemCountPerPage = req.itemCountPerPage || this.itemCountPerPage;
		this.currentPage = req.currentPage || this.currentPage;
		this.listPageCount = req.listPageCount || this.listPageCount;
		
		this.init(req);
	},
	init:function(req){
		if(this.items){
			this.itemCount = this.items.length;
			//初始化有多少页
			var pageCount = this.itemCount / this.itemCountPerPage;
			this.pageCount = (this.itemCount % this.itemCountPerPage == 0) ? pageCount : Math.ceil(pageCount);
		}
	},
	setCurrentPage: function(currentPage){
		this.currentPage = currentPage;
		// 当前页不能比总分页数大
		if (this.currentPage > this.pageCount) {
			this.currentPage = this.pageCount;
		}
		// 当前页不能比1小
		if (this.currentPage < 1) {
			this.currentPage = 1;
		}
	},
	getCurrentPage: function() {
		// 当前页不能比总分页数大
		if (this.currentPage > this.pageCount) {
			this.currentPage = this.pageCount;
		}
		// 当前页不能比1小
		if (this.currentPage < 1) {
			this.currentPage = 1;
		}
		return this.currentPage;
	},
	getStartPoint: function() {
		var result = (this.getCurrentPage()-1) * this.itemCountPerPage;
		if (result < 0) {
			return 0;
		}
		return result;
	},
	getEndPoint: function(){
		var result = this.getStartPoint() + this.itemCountPerPage;
		if(this.itemCount >= result){
			return result;
		}
		else{
			return this.itemCount;
		}
	},
	getListPageStart: function() {
		var listPageStart = 0;
		if (this.getCurrentPage() < this.listPageCount) {
			listPageStart = 1;
		} else if (this.getCurrentPage() > (this.pageCount - this.listPageCount / 2)){
			listPageStart = (this.pageCount - this.listPageCount + 1);
		} else {
			listPageStart = (this.getCurrentPage() - this.listPageCount / 2 + 1);
		}
		return listPageStart;
	},
	getListPageEnd: function(){
		var listPageEnd = 0;
		if (this.getCurrentPage() < this.listPageCount) {
			listPageEnd = this.listPageCount;
		} else {
			listPageEnd = (this.getCurrentPage() + this.listPageCount / 2);
		}
		
		if (listPageEnd < this.pageCount) {
			return listPageEnd;
		} else {
			return this.pageCount;
		}
	},
	getCurrentItems:function(){
		console.log("s->"+this.getStartPoint());
		console.log("e->"+this.getEndPoint());
		return this.items.slice(this.getStartPoint(),this.getEndPoint());
	}
});
